﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using 智慧无纸化会议客户端软件.Code;

namespace 智慧无纸化会议客户端软件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitForm();
            Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            if (EventAction.ExitAction != null) EventAction.ExitAction();
        }

        public void AddChildControl(UIElement obj)
        {
            this.viewbox1.Child = obj;
        }

        public void InitExitAction(Action action)
        {
            EventAction.ExitAction = action;
        }

        public void getAppInfo()
        {
            _ClientPublish = Common.GetConfig<ClientPublish>("clientPublish") ?? new ClientPublish();
            if (string.IsNullOrEmpty(_ServiceUrl)) return;
            try
            {
                long time = 0;
                int count = 0;
                do
                {
                    time = RequestService.GetCurrentTime(_ServiceUrl);
                    Thread.Sleep(2000);
                    count++;
                } while (time == 0 && count < 3);
               
                if (time == 0) return;

                var list = RequestService.GetClientPublish(_ClientPublish.CreateDate);
                if (list != null && list.Count > 0)
                {
                    foreach (var clientPublish in list)
                    {
                        if (_ClientPublish.DllPath != clientPublish.DllPath && _ClientPublish.CreateDate != clientPublish.CreateDate)
                        {
                            if (_ClientPublish.DllPath != clientPublish.DllPath) getZipFile(clientPublish.DllPath);

                            _ClientPublish.DllPath = clientPublish.DllPath;
                            _ClientPublish.CreateDate = clientPublish.CreateDate;
                            Common.SaveConfig<ClientPublish>("clientPublish", _ClientPublish);
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void getZipFile(string zipPath)
        {
            if (string.IsNullOrEmpty(zipPath)) return;

            try
            {
                string url = string.Concat(_ServiceUrl, zipPath);
                Common.DeCompress(Common.GetHttpFileBytes(url), AppDomain.CurrentDomain.BaseDirectory);
            }
            catch (Exception ex)
            {
                ex= Common.GetInnerException(ex);
                MessageBox.Show(ex.Message, "提示信息");
            }
        }


        Dictionary<string, object> getClientConfig()
        {
            string serial = Common.ToBase64String(LocalUniqueId.GetValue());
            var config = Common.GetConfig<Dictionary<string, object>>("clientConfig");
            if (config != null && config.GetStringValue("Serial") != serial) config = null;
            if (config == null)
            {
                config = new Dictionary<string, object>();
                config["Serial"] = serial;
                config["Mac"] = Common.GetMacAddress();

                Common.SaveConfig<Dictionary<string, object>>("clientConfig", config);
            }
            return config;
        }

        ClientPublish _ClientPublish { get; set; }

        string _ServiceUrl { get; set; }

        private void InitForm()
        {
            try
            {
                var config = getClientConfig();
                _ServiceUrl = config.GetStringValue("ServiceUrl");
                if (string.IsNullOrEmpty(_ServiceUrl))
                {
                    _ServiceUrl = AppSetting.ServiceUrl;
                    config["ServiceUrl"] = _ServiceUrl;
                }

                getAppInfo();

                string appName = Common.GetAppSetting("AppName");

                var type1 = Common.GetClassType("Yxinmt.Windows." + appName, "MainWindow2");

                Activator.CreateInstance(type1);

                var type2 = Common.GetClassType("Yxinmt.Windows", "MainWindow");
                string appPath = string.Empty;
                Action<Action> initEventAction = (a) => InitExitAction(a);
                Activator.CreateInstance(type2, this, "Index", typeof(DefaultWindow), config, initEventAction);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }
        }
    }
}
